import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '../views/Layout'
import Home from '../views/Home'
import Search from '../views/Search'
import Play from '../views/Play'
import Login from '../views/Login'
import Reg from '../views/Reg'
import User from '../views/User'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/layout'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/reg',
    component: Reg
  },
  {
    path: '/layout',
    component: Layout,
    redirect: '/home',
    children: [
      {
        path: '/home',
        component: Home,
        // 配置路由规则额外的信息
        meta: {
          title: '首页'
        }
      },
      {
        path: '/search',
        component: Search,
        meta: {
          title: '搜索'
        }
      },
      {
        path: '/user',
        component: User,
        meta: {
          title: '个人中心'
        }
      }
    ]
  },
  {
    path: '/play/:id',
    component: Play
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  // const token = localStorage.getItem('wyy-token')
  // if (to.path === '/user' && !token) {
  //   console.log(123)
  //   return next('/login')
  // }
  next()
})

export default router
